import os
import sys

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from utils.toml import load_mysql_config
from dao.orm import DBManager

mysql_config = load_mysql_config("./config/config.toml")

db = DBManager(
    mysql_config["host"],
    mysql_config["user"],
    mysql_config["password"],
    mysql_config["database"]
)

class Exam:
    def __init__(self):
        self.db = db.connect()

    def get_exam(self, user_id):
        sql = f'SELECT * FROM exam_schedule WHERE teacher_id="{user_id}"'
        print(sql)
        return db.fetchall(sql)
    
    def get_calss_exam(self, class_name):
        sql = f'SELECT * FROM exam_schedule WHERE class_name="{class_name}"'
        return db.fetchall(sql)
    
    def get_calss_by_exam(self, exam_name):
        sql = f'SELECT * FROM exam_schedule WHERE exam_name="{exam_name}"'
        return db.fetchone(sql)

    # 写入数据库
    def write_exam(self, exam):
        return db.insert('exam_schedule', exam)
    
    # 更新数据库
    def update_exam(self, exam):
        return db.insert('exam_schedule', exam)